package com.kwin.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SessionApp23 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        HttpSession httpSession = req.getSession();
        List<Book> list = (List) httpSession.getAttribute("list");
        if (list == null || list.size() == 0) {
            resp.getWriter().write("对不起，你没有买过任何商品");
        } else {
            resp.getWriter().write("你购买过以下商品");
            resp.getWriter().write("<br/>");
            for (Book book:list) {
                resp.getWriter().write(book.getName());
                resp.getWriter().write("<br/>");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
